import { httpClient } from '@/api'
import { Pagination, Pageable, Response } from '@/types'

/**
 * 分页查询模版
 * @param url 接口地址
 * @param request 请求参数
 * @param pageable 分页参数
 */
const findPage = async <RequestType, ViewType>(
  url: string,
  request: RequestType,
  pageable: Pageable
): Promise<Response<Pagination<ViewType>>> => {
  const res = await httpClient.get<Response<Pagination<ViewType>>>(url, {
    params: { ...request, ...pageable },
  })
  return res.data
}

/**
 * 新增模版
 * @param url 接口地址
 * @param form  表单数据
 */
const add = async <FormType>(
  url: string,
  form: FormType
): Promise<Response<string>> => {
  const res = await httpClient.post<Response<string>>(url, form)
  return res.data
}

/**
 * 更新模版
 * @param url 接口地址
 * @param form 表单数据
 */
const update = async <FormType>(
  url: string,
  form: FormType
): Promise<Response<string>> => {
  const res = await httpClient.post<Response<string>>(url, form)
  return res.data
}

/**
 * 根据id删除模版
 * @param url 接口地址
 * @param id 主键
 */
const deleteById = async (
  url: string,
  id: string
): Promise<Response<string>> => {
  const res = await httpClient.post<Response<string>>(url, null, {
    params: { id },
  })
  return res.data
}

/**
 * 请求模版
 */
export const requestTemplate = {
  findPage,
  add,
  update,
  deleteById,
}
